#include "GlActionChain.h"
#include "GlGuiBasicEvent.h"



GlActionChain::GlActionChain (GlWidget * pRelatedWidget)
  : _pRelatedEvent (NULL), _pRelatedWidget (_pRelatedWidget) {}



GlActionChain::GlActionChain (GlActionChain & arg)
  : _actions (arg._actions), _pRelatedWidget (arg._pRelatedWidget)
{
  setRelatedEvent (arg._pRelatedEvent);
}



GlActionChain::GlActionChain (GlWidget * pRelatedWidget, int eventType)
  : _pRelatedWidget (pRelatedWidget)
{
  _pRelatedEvent = new GlGuiBasicEvent (_pRelatedWidget, eventType);
}



GlActionChain::~GlActionChain ()
{
  for (size_t i = 0; i < _actions.size (); ++i)
  {
    delete _actions[i];
  }
  delete _pRelatedEvent;
}



void GlActionChain::addAction(GlAction * pAction) 
{
  _actions.push_back (pAction);
}



void GlActionChain::performActions(GlGuiEvent & e) 
{
  if (_pRelatedEvent != NULL)
  {
    _pRelatedEvent->setCauseEvent (&e);
    for (size_t i = 0; i < _actions.size (); ++i)
    {
      _actions[i]->performAction (*_pRelatedEvent);
    }
  }
  else
  {
    for (size_t i = 0; i < _actions.size (); ++i)
    {
      _actions[i]->performAction (e);
    }
  }
}



void GlActionChain::setRelatedEvent (const GlGuiEvent * pRelatedEvent)
{
  if (_pRelatedEvent != NULL)
    delete _pRelatedEvent;
  _pRelatedEvent = new GlGuiBasicEvent (*(GlGuiBasicEvent *)pRelatedEvent);
}
